In [2]:
import sys
from ete3 import Tree, TreeStyle, NodeStyle
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import math

# Creates a tree
file="delta_species_tree.txt"

try:
    f=open(file, 'r')
except IOError:
    print ("Unknown file: "+file)
    sys.exit()

line = ""
for l in f:
    line += l.strip()
    
f.close()

t = Tree( line )

In [3]:
def getNameToLeavesLink( t ):
    node2leaves = t.get_cached_content()
    nodeId2LeafList = dict()
    leafList2NodeId = dict()
    for k in node2leaves.keys():
        if len(node2leaves[k]) == 1: #leaf node
            pass
        else:
            nodelist = list()
            for n in node2leaves[k]:
                nodelist.append( n.name )
            nodelist.sort()
            nodeId2LeafList[k.support] = nodelist
            leafList2NodeId[tuple(nodelist)] = int(k.support) # Need to transform the mutable list into an immutable tuple
    return nodeId2LeafList, leafList2NodeId

nodeId2LeafListRef, leafList2NodeIdRef = getNameToLeavesLink( t )

In [4]:
def renumberNodes( treeToAnnotate, leafList2NodeId ):
    #print treeToAnnotate.get_ascii(attributes=[ "name"], show_internal=False)
    node2leaves = treeToAnnotate.get_cached_content()
    for k in node2leaves.keys():
        if len(node2leaves[k]) == 1: #leaf node
            pass
        else:
            nodelist = list()
            for n in node2leaves[k]:
                nodelist.append( n.name )
            nodelist.sort()
            k.support = leafList2NodeId[tuple(nodelist)]

Let's renumber the loss tree


In [5]:
file="Losses_Ultra.tree"

try:
    f=open(file, 'r')
except IOError:
    print ("Unknown file: "+file)
    sys.exit()

line = ""
for l in f:
    line += l.strip()
    
f.close()

treeToAnnotate = Tree( line )


renumberNodes(treeToAnnotate, leafList2NodeIdRef)
#print (treeToAnnotate.write(features=["support"], format=2) )
print (treeToAnnotate.write( format=2) )


((HalmarSJ:0.811812,(BdeexoJS:0.582583,(BdebacW:0.411411,(Bdebacst:0.239239,Bdebac:0.239239)65:0.172172)66:0.171171)67:0.229229)85:0.188188,((((SorcelSoe7:0.377377,SorcelSo:0.377377)89:0.33033,HalochDS:0.707708)99:0.216216,(((((MyxxanDK:0.239239,MyxfulHW:0.239239)88:0.238238,(MyxstiDS:0.287287,Myxful12:0.287287)87:0.19019)98:0.123123,CorcorDS:0.600601)105:0.133133,Arcgep:0.733734)108:0.107107,(AnaspFw:0.471471,(Anadeh2Ca8:0.277277,(Anadeh2C:0.165165,AnaspK:0.165165)62:0.112112)63:0.194194)64:0.369369)111:0.083083)114:0.056056,(((PelcarDS:0.511512,Geosub:0.511512)83:0.34034,((PelproDS:0.46046,GeolovSZ:0.46046)81:0.308308,((Geopic:0.488488,((GeosulPC:0.116116,GeosulKN:0.116116)84:0.237237,GeometGS:0.353353)96:0.135135)103:0.189189,((GeouraRf:0.352352,GeodalFR:0.352352)80:0.221221,(GeospM1:0.377377,(GeospM2:0.197197,GeobemBe:0.197197)79:0.18018)82:0.196196)95:0.104104)106:0.091091)109:0.083083)112:0.076076,(((SynaciSB:0.551552,DestieDS:0.551552)77:0.29029,((((DesoleHx:0.432432,(DestolTo:0.246246,DesautHR:0.246246)71:0.186186)74:0.162162,DesalkAK:0.594595)91:0.151151,((((DessulDS:0.233233,DespsyLS:0.233233)75:0.142142,DesproDS:0.375375)92:0.125125,DesalkAH:0.500501)100:0.169169,((DesretDS:0.369369,DesbacDS:0.369369)73:0.226226,((((Desvulstn6:0.279279,((Desvulst:0.097097,DesvulRC:0.097097)78:0.069069,DesvulDP:0.166166)94:0.113113)102:0.088088,((LawintPH:0.051051,LawintN3:0.051051)86:0.149149,Desdessu:0.2002)97:0.167167)104:0.082082,DesalaG2:0.449449)107:0.092092,(DesmagRS:0.457457,(Desafrst:0.363363,(DessalDS:0.249249,(DesdesND:0.13013,DesaesAs:0.13013)70:0.119119)76:0.114114)93:0.094094)101:0.084084)110:0.054054)113:0.074074)115:0.076076)116:0.053053,(DesbaaDS:0.602603,(SynfumMP:0.347347,DesaceDS:0.347347)69:0.255255)72:0.196196)117:0.043043)118:0.031031,((HipmarDS:0.293293,DesaceA6:0.293293)68:0.192192,CanBabma:0.485485)90:0.387387)119:0.055055)120:0.052052)121:0.02002);

Now the transfer tree


In [6]:
file="Transfers_Ultra.tree"

try:
    f=open(file, 'r')
except IOError:
    print ("Unknown file: "+file)
    sys.exit()

line = ""
for l in f:
    line += l.strip()
    
f.close()

treeToAnnotate = Tree( line )


renumberNodes(treeToAnnotate, leafList2NodeIdRef)
#print (treeToAnnotate.write(features=["support"], format=2) )
print (treeToAnnotate.write( format=2) )


((HalmarSJ:0.766767,(BdeexoJS:0.520521,(BdebacW:0.34034,(Bdebacst:0.141141,Bdebac:0.141141)65:0.199199)66:0.18018)67:0.246246)85:0.233233,((((SorcelSoe7:0.289289,SorcelSo:0.289289)89:0.316316,HalochDS:0.605606)99:0.215215,(((((MyxxanDK:0.186186,MyxfulHW:0.186186)88:0.132132,(MyxstiDS:0.177177,Myxful12:0.177177)87:0.141141)98:0.112112,CorcorDS:0.43043)105:0.123123,Arcgep:0.553554)108:0.151151,(AnaspFw:0.462462,(Anadeh2Ca8:0.266266,(Anadeh2C:0.162162,AnaspK:0.162162)62:0.104104)63:0.196196)64:0.242242)111:0.116116)114:0.129129,(((PelcarDS:0.502503,Geosub:0.502503)83:0.297297,((PelproDS:0.459459,GeolovSZ:0.459459)81:0.24024,((Geopic:0.471471,((GeosulPC:0.128128,GeosulKN:0.128128)84:0.206206,GeometGS:0.334334)96:0.137137)103:0.13013,((GeouraRf:0.299299,GeodalFR:0.299299)80:0.177177,(GeospM1:0.304304,(GeospM2:0.165165,GeobemBe:0.165165)79:0.139139)82:0.172172)95:0.125125)106:0.098098)109:0.1001)112:0.111111,(((SynaciSB:0.557558,DestieDS:0.557558)77:0.263263,((((DesoleHx:0.441441,(DestolTo:0.264264,DesautHR:0.264264)71:0.177177)74:0.127127,DesalkAK:0.568569)91:0.153153,((((DessulDS:0.241241,DespsyLS:0.241241)75:0.144144,DesproDS:0.385385)92:0.141141,DesalkAH:0.526527)100:0.139139,((DesretDS:0.384384,DesbacDS:0.384384)73:0.208208,((((Desvulstn6:0.247247,((Desvulst:0.056056,DesvulRC:0.056056)78:0.082082,DesvulDP:0.138138)94:0.109109)102:0.102102,((LawintPH:0.071071,LawintN3:0.071071)86:0.163163,Desdessu:0.234234)97:0.115115)104:0.077077,DesalaG2:0.426426)107:0.099099,(DesmagRS:0.437437,(Desafrst:0.348348,(DessalDS:0.245245,(DesdesND:0.137137,DesaesAs:0.137137)70:0.108108)76:0.103103)93:0.089089)101:0.088088)110:0.067067)113:0.073073)115:0.056056)116:0.05005,(DesbaaDS:0.606607,(SynfumMP:0.402402,DesaceDS:0.402402)69:0.204204)72:0.165165)117:0.049049)118:0.055055,((HipmarDS:0.352352,DesaceA6:0.352352)68:0.329329,CanBabma:0.681682)90:0.194194)119:0.035035)120:0.039039)121:0.05005);

Now the substitutions tree:


In [7]:
file="Substitutions_Ultra.tree"

try:
    f=open(file, 'r')
except IOError:
    print ("Unknown file: "+file)
    sys.exit()

line = ""
for l in f:
    line += l.strip()
    
f.close()

treeToAnnotate = Tree( line )


renumberNodes(treeToAnnotate, leafList2NodeIdRef)
#print (treeToAnnotate.write(features=["support"], format=2) )
print (treeToAnnotate.write( format=2) )


((HalmarSJ:0.893894,(BdeexoJS:0.743744,(BdebacW:0.623624,(Bdebacst:0.45045,Bdebac:0.45045)65:0.173173)66:0.12012)67:0.15015)85:0.106106,((((SorcelSoe7:0.552553,SorcelSo:0.552553)89:0.267267,HalochDS:0.81982)99:0.134134,(((((MyxxanDK:0.492492,MyxfulHW:0.492492)88:0.177177,(MyxstiDS:0.494494,Myxful12:0.494494)87:0.175175)98:0.079079,CorcorDS:0.748749)105:0.071071,Arcgep:0.81982)108:0.083083,(AnaspFw:0.788789,(Anadeh2Ca8:0.667668,(Anadeh2C:0.495495,AnaspK:0.495495)62:0.172172)63:0.121121)64:0.114114)111:0.051051)114:0.039039,(((PelcarDS:0.701702,Geosub:0.701702)83:0.233233,((PelproDS:0.66967,GeolovSZ:0.66967)81:0.22022,((Geopic:0.758759,((GeosulPC:0.469469,GeosulKN:0.469469)84:0.18018,GeometGS:0.64965)96:0.109109)103:0.091091,((GeouraRf:0.582583,GeodalFR:0.582583)80:0.198198,(GeospM1:0.650651,(GeospM2:0.477477,GeobemBe:0.477477)79:0.173173)82:0.13013)95:0.069069)106:0.04004)109:0.045045)112:0.041041,(((SynaciSB:0.736737,DestieDS:0.736737)77:0.212212,((((DesoleHx:0.682683,(DestolTo:0.485485,DesautHR:0.485485)71:0.197197)74:0.116116,DesalkAK:0.798799)91:0.114114,((((DessulDS:0.473473,DespsyLS:0.473473)75:0.179179,DesproDS:0.652653)92:0.121121,DesalkAH:0.773774)100:0.119119,((DesretDS:0.635636,DesbacDS:0.635636)73:0.196196,((((Desvulstn6:0.630631,((Desvulst:0.378378,DesvulRC:0.378378)78:0.14014,DesvulDP:0.518519)94:0.112112)102:0.077077,((LawintPH:0.397397,LawintN3:0.397397)86:0.202202,Desdessu:0.5996)97:0.108108)104:0.045045,DesalaG2:0.752753)107:0.051051,(DesmagRS:0.742743,(Desafrst:0.666667,(DessalDS:0.561562,(DesdesND:0.395395,DesaesAs:0.395395)70:0.166166)76:0.105105)93:0.076076)101:0.061061)110:0.028028)113:0.061061)115:0.02002)116:0.017017,(DesbaaDS:0.80981,(SynfumMP:0.61962,DesaceDS:0.61962)69:0.19019)72:0.12012)117:0.019019)118:0.014014,((HipmarDS:0.526527,DesaceA6:0.526527)68:0.28028,CanBabma:0.806807)90:0.156156)119:0.013013)120:0.017017)121:0.007007);

In [ ]: